package com.yunxiangyue.CreateList

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase


//数据库版本号必能随意修改
@Database(version = 1 , entities = [MusicListItem::class])
abstract class MusicDataBase:RoomDatabase() {
        abstract fun MusicListDao(): MusicListDao


        companion object{
            private var instance: MusicDataBase? = null

            @Synchronized
            fun getMusicDatabase(context: Context): MusicDataBase {
                instance?.let {
                    return it
                }

                return Room.databaseBuilder(context.applicationContext, MusicDataBase::class.java,"music_databse").allowMainThreadQueries()
//                    .addMigrations(Migration1To2())
                    .build().apply {
                        instance = this
                    }
            }

        }


    }



//class Migration1To2 : Migration(1, 2) {
//    override fun migrate(database: SupportSQLiteDatabase) {
//        // 在这里编写将数据库从版本1迁移到版本2所需的SQL语句
////        database.execSQL("ALTER TABLE MusicListItem DROP PRIMARY KEY;")
////        database.execSQL("ALTER TABLE MusicListItem ADD COLUMN id INTEGER PRIMARY KEY;")
//        // 其他必要的迁移步骤
//
//        database.execSQL("CREATE TABLE Music (" +
//                "id INTEGER PRIMARY KEY," +
//                "phone TEXT NOT NULL," +
//                "imgResource BLOB," +
//                "musicTitle TEXT," +
//                "info TEXT" +
//                ");")
//
////        database.execSQL("drop table MusicListItm;")
//
//        database.execSQL("alter table  Music rename to MusicListItm;")
//    }
//}
